package com.ck.scan.factory;

import com.ck.scan.proxy.CkInterfaceProxy;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

/**
 * TODO
 *
 * @author chenke
 * @version 1.0
 * @date 2021-12-22 14:51
 */
public class CkFactoryBean<T> implements FactoryBean<T> {

    private Class<T> ckInterface;

    public CkFactoryBean() {
    }

    public CkFactoryBean(Class<T> ckInterface) {
        this.ckInterface = ckInterface;
    }

    /**
     * bean实例化对象,指向代理类即可
     */
    @Override
    public T getObject() throws Exception {
        return (T) Proxy.newProxyInstance(ckInterface.getClassLoader(), new Class[]{ckInterface}, new CkInterfaceProxy<>(ckInterface));
    }

    /**
     * bean对象类型
     */
    @Override
    public Class<T> getObjectType() {
        return this.ckInterface;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}

